<?php
namespace app\index\model;

use think\Model;
use think\Db;
use \think\Session;
use \app\index\model\FileManage;
use \app\index\model\Option;
use \app\index\model\User;

class ShareBlackboardHandler extends Model{

	public static $SPERATOR = ',';
	public $shareData;
	public $querryStatus = true;
	public $shareOwner;
	public $fileData;
	public $dirData;
	public $lockStatus = false;

	public function __construct($id,$notExist=true){
		$this->shareData = Db::name('share_blackboard')->where('id',$id)->find();
		if(empty($this->shareData)){
			$this->querryStatus = false;
		}

	}

	static function setSuccess($text){
		die('{ "result": "'.$text.'" }');
	}
	static function setError($text){
		die('{ "result": { "success": false, "error": "'.$text.'" } }');
	}

	static function createBackbordShare($fileIds){

		if ($fileIds == null || count($fileIds) == 0) {
			self::setError("需要分享的文件为空");
		}
		$fileIdsStr = '';
		for ($i=0; $i < count($fileIds); $i++) { 
			if ($i == count($fileIds) - 1) {
				$fileIdsStr = $fileIdsStr.strval($fileIds[$i]);
				continue;
			}
			$fileIdsStr = $fileIdsStr.strval($fileIds[$i]).self::$SPERATOR;
		}
		$SQLData = [
			'share_time' => date("Y-m-d H:i:s"),
			'view_num' => 0,
			'file_ids' => $fileIdsStr,
        ];
        $id = Db::name('share_blackboard')->insertGetId($SQLData);
		if (!is_null($id)) {
            self::setSuccess(Option::getValue("siteURL")."BlackboardShare/Query/".$id);
		}
		self::setError("DB异常，分享板书失败");
	}
}
?>